package com.secure.orm.controller.thirdPay;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.secure.common.util.PayUtil;
import com.secure.orm.controller.BaseController;
import com.secure.orm.enums.PayType;
import com.secure.orm.enums.ThirdPayNotifyType;
import com.secure.orm.model.PayOrder;
import com.secure.orm.model.PaySource;
import com.secure.orm.model.response.ViewData;
import com.secure.orm.service.PayOrderService;
import com.secure.orm.service.PaySourceService;
import com.secure.orm.service.billPay.NotifyService;
import com.secure.orm.thirdPay.BaseThirdPay;
import com.secure.orm.thirdPay.alipay.Alipay;
import com.secure.orm.thirdPay.bill99.Bill99Config;

@Controller
@RequestMapping("/third")
public class Bill99Controller extends BaseController {
	protected Log logger = LogFactory.getLog(Bill99Controller.class);

	@Autowired
    private BaseThirdPay baseThirdPay;
	@Autowired
	private NotifyService notifyService;
	@Autowired
	private PayOrderService payOrderService;
	@Autowired
	private PaySourceService paySourceService;

	@RequestMapping(value = "bill99/notify",method = RequestMethod.POST)
	public @ResponseBody String notifyBill99(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> params = PayUtil.requestParams2Map(request);
		logger.info(("NotifyBill99" + PayType.Bill99.name() + ", orderId:" + params.get(Alipay.ALIPAY_OUT_TRADE_NO) + ", tradeStatus:" + params.get(Alipay.ALIPAY_TRADE_STATUS)));
		ViewData vd = null;
		try {
			vd = notifyService.handleNotify(params,PayType.Bill99,ThirdPayNotifyType.NOTIFY);
		} catch (Exception e) {
			logger.error("NotifyAlipay" + e);
			return Alipay.NOTIFY_SUCCESS_RESULT;
		}
		return vd.getMsg();
	}
	
	@RequestMapping("bill99/return")
	public String returnBill99(HttpServletRequest request,HttpServletResponse response,Model model) {
		Map<String, String> params = PayUtil.requestParams2Map(request);
		String orderId = params.get(Bill99Config.out_trade_no);
		logger.info(("ReturnAlipay" + PayType.Bill99.name() + ", orderId:" + params.get(Bill99Config.out_trade_no) + ", tradeStatus:" + params.get(Bill99Config.trade_status)));
		ViewData vd = baseThirdPay.handleResponse(params, PayType.getThirdPayClassName(PayType.Bill99), ThirdPayNotifyType.RETURN);
		String returnUrl = "http://www.b5m.com";
		if(vd.getCode()==0){
			PayOrder po = payOrderService.findByOrderId(orderId);
			if (po != null) {
				PaySource ps = paySourceService.findById(Long.valueOf(po.getSourceId()));
				if (ps != null) {
					returnUrl = ps.getReturnUrl();
				}
			}
		}
		return "redirect:"+returnUrl;
	}

}
